% this code is to make regset for daily ivol and jdnv (5huf is missing)
clear
clc
% longer accumulation
load(using 12 hr measures and daily frequency implied volatility data)

nc=18;
nd=size(datemap,1); % # of days 
%% 1. extract ivol

ivolatmmon=nan(nd,nc);
ivolotmpmon=nan(nd,nc);

% atm
tp=ivolraw(find(ivolraw(:,7)==0),:);    % ivol data from Bloomberg
for i=1:nc
    temp=tp(find(tp(:,8)==i),:);
    for j=1:nd
        tpday=max(find(temp(:,6)==datemap(j,1)));
        if tpday>0
            ivolatmmon(j,i)=temp(tpday,5);
        end
    end
end
clear i j tpday tp temp

% otm put
tp=ivolraw(find(ivolraw(:,7)==3),:);
for i=1:nc
    temp=tp(find(tp(:,8)==i),:);
    for j=1:nd
        tpday=max(find(temp(:,6)==datemap(j,1)));
        if tpday>0
            ivolotmpmon(j,i)=temp(tpday,5);
        end
    end
end
clear i j tpday tp temp

%% 2. make fomc driven jump vol and indicator

nt=size(indicator,1);   % total # of intraday obs.

fomcjdnv=nan(nd,nc);
fomcjupv=nan(nd,nc);
fomcjrv=nan(nd,nc);
fomcvol=nan(nd,nc);

indifomcday=zeros(nd,5);

for j=2:nt
    if usfomc(j,1)>0 && usfomc(j-1,1)>0
        fomcjdnv(indicator(j,4),:)=hwjdnv(hlfwinnumber(j,1),1:nc);
        fomcjupv(indicator(j,4),:)=hwjupv(hlfwinnumber(j,1),1:nc);
        fomcjrv(indicator(j,4),:)=hwjrvol(hlfwinnumber(j,1),1:nc);
        fomcvol(indicator(j,4),:)=hwvol(hlfwinnumber(j,1),1:nc);

        indifomcday(indicator(j,4)-2,1)=1;
        indifomcday(indicator(j,4)-1,2)=1;
        indifomcday(indicator(j,4),3)=1;
        indifomcday(indicator(j,4)+1,4)=1;
        indifomcday(indicator(j,4)+2,5)=1;
    end
end
clear j

%% 3. make ivol changes

chgfomcone=nan(nd,nc*4);
chgpreone=nan(nd,nc*4);
chgpostone=nan(nd,nc*4);

% the above shows one day changes in ivol for three days centered around
% FOMC days

for j=3:nd-3
    if indifomcday(j,3)>0
        chgfomcone(j,nc+1:2*nc)=ivolatmmon(j,:)-ivolatmmon(j-1,:);
        chgpreone(j,nc+1:2*nc)=ivolatmmon(j-1,:)-ivolatmmon(j-2,:);
        chgpostone(j,nc+1:2*nc)=ivolatmmon(j+1,:)-ivolatmmon(j,:);

        chgfomcone(j,3*nc+1:4*nc)=ivolotmpmon(j,:)-ivolotmpmon(j-1,:);
        chgpreone(j,3*nc+1:4*nc)=ivolotmpmon(j-1,:)-ivolotmpmon(j-2,:);
        chgpostone(j,3*nc+1:4*nc)=ivolotmpmon(j+1,:)-ivolotmpmon(j,:);
    end
end
clear j

%% 4. ivol change report (panels a and b)

selectjdn=nan(nd,nc*4);
selectnojdn=nan(nd,nc*4);

tempone=nan(nd,nc);
temptwo=ones(nd,nc);

for i=1:nc
    for j=1:nd
        if fomcjdnv(j,i)>0
            tempone(j,i)=1;
            temptwo(j,i)=nan;
        end
    end
end
clear i j

selectjdn=[tempone tempone tempone tempone];
selectnojdn=[temptwo temptwo temptwo temptwo];
%none= # of jump FOMC x effective currencies
%ntwo= # of no jump FOMC x effective currencies
%nthr= # of FOMC x effective currencies

repivone=nan(4,10);
temp=selectjdn.*chgpreone;
tppreonejp=nanmean(temp(:,nc+1:2*nc),2);
repivone(1,1)=nanmean(tppreonejp,1);
repivone(1,5)=sqrt(none)*repivone(1,1)/nanstd(tppreonejp,1);

temp=selectjdn.*chgfomcone;
tpfomconejp=nanmean(temp(:,nc+1:2*nc),2);
repivone(1,2)=nanmean(tpfomconejp,1);
repivone(1,3)=sqrt(none)*repivone(1,2)/nanstd(tpfomconejp,1);

temp=selectjdn.*chgpostone;
tppostonejp=nanmean(temp(:,nc+1:2*nc),2);
repivone(1,4)=nanmean(tppostonejp,1);

temp=selectjdn.*chgpreone;
tpppreonejp=nanmean(temp(:,3*nc+1:4*nc),2);
repivone(1,6)=nanmean(tpppreonejp,1);
repivone(1,10)=sqrt(none)*repivone(1,6)/nanstd(tpppreonejp,1);

temp=selectjdn.*chgfomcone;
tppfomconejp=nanmean(temp(:,3*nc+1:4*nc),2);
repivone(1,7)=nanmean(tppfomconejp,1);
repivone(1,8)=sqrt(none)*repivone(1,7)/nanstd(tppfomconejp,1);

temp=selectjdn.*chgpostone;
tpppostonejp=nanmean(temp(:,3*nc+1:4*nc),2);
repivone(1,9)=nanmean(tpppostonejp,1);

temp=selectnojdn.*chgpreone;
tppreonenjp=nanmean(temp(:,nc+1:2*nc),2);
repivone(2,1)=nanmean(tppreonenjp,1);
repivone(2,5)=sqrt(ntwo)*repivone(2,1)/nanstd(tppreonenjp,1);

temp=selectnojdn.*chgfomcone;
tpfomconenjp=nanmean(temp(:,nc+1:2*nc),2);
repivone(2,2)=nanmean(tpfomconenjp,1);
repivone(2,3)=sqrt(ntwo)*repivone(2,2)/nanstd(tpfomconenjp,1);

temp=selectnojdn.*chgpostone;
tppostonenjp=nanmean(temp(:,nc+1:2*nc),2);
repivone(2,4)=nanmean(tppostonenjp,1);

temp=selectnojdn.*chgpreone;
tpppreonenjp=nanmean(temp(:,3*nc+1:4*nc),2);
repivone(2,6)=nanmean(tpppreonenjp,1);
repivone(2,10)=sqrt(ntwo)*repivone(2,6)/nanstd(tpppreonenjp,1);

temp=selectnojdn.*chgfomcone;
tppfomconenjp=nanmean(temp(:,3*nc+1:4*nc),2);
repivone(2,7)=nanmean(tppfomconenjp,1);
repivone(2,8)=sqrt(ntwo)*repivone(2,7)/nanstd(tppfomconenjp,1);

temp=selectnojdn.*chgpostone;
tpppostonenjp=nanmean(temp(:,3*nc+1:4*nc),2);
repivone(2,9)=nanmean(tpppostonenjp,1);

repivone(3,:)=repivone(1,:)-repivone(2,:);
repivone(4,1)=sqrt(nthr)*repivone(3,1)/sqrt(nanstd(tppreonejp,1)^2+nanstd(tppreonenjp)^2);
repivone(4,2)=sqrt(nthr)*repivone(3,2)/sqrt(nanstd(tpfomconejp,1)^2+nanstd(tpfomconenjp)^2);
repivone(4,4)=sqrt(nthr)*repivone(3,4)/sqrt(nanstd(tppostonejp,1)^2+nanstd(tppostonenjp)^2);

repivone(4,6)=sqrt(nthr)*repivone(3,6)/sqrt(nanstd(tpppreonejp,1)^2+nanstd(tpppreonenjp)^2);
repivone(4,7)=sqrt(nthr)*repivone(3,7)/sqrt(nanstd(tppfomconejp,1)^2+nanstd(tppfomconenjp)^2);
repivone(4,9)=sqrt(nthr)*repivone(3,9)/sqrt(nanstd(tpppostonejp,1)^2+nanstd(tpppostonenjp)^2);

%%% for positive jump

selectjup=nan(nd,nc*4);
selectnojup=nan(nd,nc*4);

tempthr=nan(nd,nc);
tempfor=ones(nd,nc);

fomcjupv(:,17)=nan;

for i=1:nc
    for j=1:nd
        if fomcjupv(j,i)>0
            tempthr(j,i)=1;
            tempfor(j,i)=nan;
        end
    end
end
clear i j

selectjup=[tempthr tempthr tempthr tempthr];
selectnojup=[tempfor tempfor tempfor tempfor];

repivthr=nan(4,10);
temp=selectjup.*chgpreone;
tppreonejp=nanmean(temp(:,nc+1:2*nc),2);
repivthr(1,1)=nanmean(tppreonejp,1);
repivthr(1,5)=sqrt(none)*repivthr(1,1)/nanstd(tppreonejp,1);

temp=selectjup.*chgfomcone;
tpfomconejp=nanmean(temp(:,nc+1:2*nc),2);
repivthr(1,2)=nanmean(tpfomconejp,1);
repivthr(1,3)=sqrt(none)*repivthr(1,2)/nanstd(tpfomconejp,1);

temp=selectjup.*chgpostone;
tppostonejp=nanmean(temp(:,nc+1:2*nc),2);
repivthr(1,4)=nanmean(tppostonejp,1);

temp=selectjup.*chgpreone;
tpppreonejp=nanmean(temp(:,3*nc+1:4*nc),2);
repivthr(1,6)=nanmean(tpppreonejp,1);
repivthr(1,10)=sqrt(none)*repivthr(1,6)/nanstd(tpppreonejp,1);

temp=selectjup.*chgfomcone;
tppfomconejp=nanmean(temp(:,3*nc+1:4*nc),2);
repivthr(1,7)=nanmean(tppfomconejp,1);
repivthr(1,8)=sqrt(none)*repivthr(1,7)/nanstd(tppfomconejp,1);

temp=selectjup.*chgpostone;
tpppostonejp=nanmean(temp(:,3*nc+1:4*nc),2);
repivthr(1,9)=nanmean(tpppostonejp,1);

temp=selectnojup.*chgpreone;
tppreonenjp=nanmean(temp(:,nc+1:2*nc),2);
repivthr(2,1)=nanmean(tppreonenjp,1);
repivthr(2,5)=sqrt(ntwo)*repivthr(2,1)/nanstd(tppreonenjp,1);

temp=selectnojup.*chgfomcone;
tpfomconenjp=nanmean(temp(:,nc+1:2*nc),2);
repivthr(2,2)=nanmean(tpfomconenjp,1);
repivthr(2,3)=sqrt(ntwo)*repivthr(2,2)/nanstd(tpfomconenjp,1);

temp=selectnojup.*chgpostone;
tppostonenjp=nanmean(temp(:,nc+1:2*nc),2);
repivthr(2,4)=nanmean(tppostonenjp,1);

temp=selectnojup.*chgpreone;
tpppreonenjp=nanmean(temp(:,3*nc+1:4*nc),2);
repivthr(2,6)=nanmean(tpppreonenjp,1);
repivthr(2,10)=sqrt(ntwo)*repivthr(2,6)/nanstd(tpppreonenjp,1);

temp=selectnojup.*chgfomcone;
tppfomconenjp=nanmean(temp(:,3*nc+1:4*nc),2);
repivthr(2,7)=nanmean(tppfomconenjp,1);
repivthr(2,8)=sqrt(ntwo)*repivthr(2,7)/nanstd(tppfomconenjp,1);

temp=selectnojup.*chgpostone;
tpppostonenjp=nanmean(temp(:,3*nc+1:4*nc),2);
repivthr(2,9)=nanmean(tpppostonenjp,1);

repivthr(3,:)=repivthr(1,:)-repivthr(2,:);
repivthr(4,1)=sqrt(nthr)*repivthr(3,1)/sqrt(nanstd(tppreonejp,1)^2+nanstd(tppreonenjp)^2);
repivthr(4,2)=sqrt(nthr)*repivthr(3,2)/sqrt(nanstd(tpfomconejp,1)^2+nanstd(tpfomconenjp)^2);
repivthr(4,4)=sqrt(nthr)*repivthr(3,4)/sqrt(nanstd(tppostonejp,1)^2+nanstd(tppostonenjp)^2);

repivthr(4,6)=sqrt(nthr)*repivthr(3,6)/sqrt(nanstd(tpppreonejp,1)^2+nanstd(tpppreonenjp)^2);
repivthr(4,7)=sqrt(nthr)*repivthr(3,7)/sqrt(nanstd(tppfomconejp,1)^2+nanstd(tppfomconenjp)^2);
repivthr(4,9)=sqrt(nthr)*repivthr(3,9)/sqrt(nanstd(tpppostonejp,1)^2+nanstd(tpppostonenjp)^2);

%% 5. correlation (panel c)

reviewa=nan(nd,nc*3);
reviewo=nan(nd,nc*2);

tpcorrsumjdnv=nan(2,nc);
for i=1:nc
    tpcorr=[fomcjdnv(:,i) chgfomcone(:,nc+i)];
    tpcorrjdnv=corrcoef(tpcorr,'Row','complete');
    tpcorrsumjdnv(1,i)=tpcorrjdnv(1,2);
    reviewa(:,(i-1)*3+1:(i-1)*3+2)=tpcorr;
    reviewa(:,3*i)=chgfomcone(:,3*nc+i);

    tpcorr=[fomcjdnv(:,i) chgfomcone(:,3*nc+i)];
    tpcorrjdnv=corrcoef(tpcorr,'Row','complete');
    tpcorrsumjdnv(2,i)=tpcorrjdnv(1,2);
    reviewo(:,(i-1)*2+1:2*i)=tpcorr;
end
clear i

nanmean(tpcorrsumjdnv,2)

rereviewa=nan(nd,nc*3);
rereviewo=nan(nd,nc*2);

tpcorrsumjupv=nan(2,nc);
for i=1:nc
    tpcorr=[fomcjupv(:,i) chgfomcone(:,nc+i)];
    tpcorrjupv=corrcoef(tpcorr,'Row','complete');
    tpcorrsumjupv(1,i)=tpcorrjupv(1,2);
    rereviewa(:,(i-1)*3+1:(i-1)*3+2)=tpcorr;
    rereviewa(:,3*i)=chgfomcone(:,3*nc+i);

    tpcorr=[fomcjupv(:,i) chgfomcone(:,3*nc+i)];
    tpcorrjupv=corrcoef(tpcorr,'Row','complete');
    tpcorrsumjupv(2,i)=tpcorrjupv(1,2);
    rereviewo(:,(i-1)*2+1:2*i)=tpcorr;
end
clear i

nanmean(tpcorrsumjupv,2)
